﻿// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_BOM_PRODUCT_BOM_MASTER
// * CreateDate    : 2012/4/1 20:15:46
// * Creater       : CodeSmith
// * LastChangeDate: 2012/4/1 20:15:46
// * LastChanger   : 2012/4/1 20:15:46
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;
using ESOL.Business.Mst;

namespace ESOL.Business.Bom
{
    /// <summary>
    /// BomProductBomMaster
    /// </summary>
    public partial class BomProductBomMasterBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objBomProductBomMasterEntity">Entity</param>
        /// <returns>Entity</returns>
        public static BomProductBomMasterEntity GetBomProductBomMaster(BomProductBomMasterEntity objBomProductBomMasterEntity)
        {
            return DataAccess.SelectSingle<BomProductBomMasterEntity>(objBomProductBomMasterEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objBomProductBomMasterEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertBomProductBomMaster(BomProductBomMasterEntity objBomProductBomMasterEntity)
        {
            return DataAccess.Insert(objBomProductBomMasterEntity);
        }

        public static bool InsertBomProductBomMaster(List<BomProductBomMasterEntity> objBomProductBomMasterEntityList)
        {
            return DataAccess.Insert(objBomProductBomMasterEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objBomProductBomMasterEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateBomProductBomMaster(BomProductBomMasterEntity objBomProductBomMasterEntity)
        {
            return DataAccess.Update(objBomProductBomMasterEntity);
        }

        public static bool UpdateBomProductBomMaster(List<BomProductBomMasterEntity> objBomProductBomMasterEntityList)
        {
            return DataAccess.Update<BomProductBomMasterEntity>(objBomProductBomMasterEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objBomProductBomMasterEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteBomProductBomMaster(BomProductBomMasterEntity objBomProductBomMasterEntity)
        {
            return DataAccess.Delete(objBomProductBomMasterEntity);
        }

        public static bool DeleteBomProductBomMaster(List<BomProductBomMasterEntity> objBomProductBomMasterEntityList)
        {
            return DataAccess.Delete<BomProductBomMasterEntity>(objBomProductBomMasterEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetBomProductBomMasterAll()
        {
            return DataAccess.SelectAllDataSet<BomProductBomMasterEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<BomProductBomMasterEntity> GetBomProductBomMasterAllList()
        {
            return DataAccess.SelectAll<BomProductBomMasterEntity>();
        }
        #endregion

        #region Functions
        public static BomProductBomMasterEntity GetProductBomMasterByID(string sID)
        {
            BomProductBomMasterEntity entity = new BomProductBomMasterEntity();
            entity.BomId = sID;
            return DataAccess.SelectSingle<BomProductBomMasterEntity>(entity);
        }
        public static bool IsExistedBom(string sProductID)
        {
            if (string.IsNullOrEmpty(sProductID) == true)
                return false;
            DataTable dt = new DataTable();
            string sql = @"select * from BOM_PRODUCT_BOM_MASTER bpbm where bpbm.PRODUCT_ID='" + sProductID + "'";
            sql += " and bpbm.PARENT_BOM_ID is NULL";
            dt = DataAccess.SelectDataSet(sql).Tables[0];
            if (dt.Rows.Count == 0)
            {
                return false;
            }
            else { return true; }
        }

        //获取产品BOM
        public static DataTable GetDtProductBomByProductID(string sProductID,string sParentItemType,string sChildItemType)
        {
            DataTable dt = new DataTable();
            string sql = @"select bpbm.*,mp.PRODUCT_NAME,mp.PRODUCT_CODE,mp.DESCRIPTION 
                from BOM_PRODUCT_BOM_MASTER bpbm
                left join MST_PRODUCT mp on bpbm.PRODUCT_ID=mp.PRODUCT_ID
                where bpbm.PARENT_BOM_ID is null
                and bpbm.VERSION='1.0'
                and bpbm.PRODUCT_ID='" + sProductID + "' ";
            sql += "and bpbm.ITEM_TYPE='" + sParentItemType + "' ";
            dt = DataAccess.SelectDataSet(sql).Tables[0];
            if (dt.Rows.Count != 0)
            {
                QueryProductBom(dt, dt.Rows[0]["BOM_ID"].ToString(), sChildItemType);
            }
            return dt;
        }
        public static DataTable GetDtProductBomByBomID(string sBomID, string sParentItemType, string sChildItemType)
        {
            DataTable dt = new DataTable();
            string sql = @"select bpbm.*,mp.PRODUCT_NAME,mp.PRODUCT_CODE,mp.DESCRIPTION  
            from BOM_PRODUCT_BOM_MASTER bpbm
            left join MST_PRODUCT mp on bpbm.PRODUCT_ID=mp.PRODUCT_ID
            where BOM_ID='" + sBomID + "' ";
            //sql += "and status='BOM_S01' and ITEM_TYPE='" + sParentItemType + "' ";
            dt = DataAccess.SelectDataSet(sql).Tables[0];
            if (dt.Rows.Count != 0)
            {
                QueryProductBom(dt, dt.Rows[0]["BOM_ID"].ToString(), sChildItemType);
            }
            return dt;
        }
        public static void QueryProductBom(DataTable dtBom, string sParentBomID, string sItemType)
        {
            string sql = @"select bpbm.*,mp.PRODUCT_NAME,mp.PRODUCT_CODE,mp.DESCRIPTION  
                from BOM_PRODUCT_BOM_MASTER bpbm
                left join MST_PRODUCT mp on bpbm.PRODUCT_ID=mp.PRODUCT_ID
                where bpbm.PARENT_BOM_ID='" + sParentBomID + "' ";
            DataTable dt = DataAccess.SelectDataSet(sql).Tables[0];
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    if (dr["ITEM_TYPE"].ToString().Equals(sItemType))
                    {
                        dtBom.ImportRow(dr);
                    }
                    else if (string.IsNullOrEmpty(sItemType))
                    {
                        dtBom.ImportRow(dr);
                    }
                    QueryProductBom(dtBom, dr["BOM_ID"].ToString(), sItemType);
                }
            }
        }

        /// <summary>
        /// 查询技术部图纸BOM
        /// </summary>
        /// <param name="sBlueprintID"></param>
        /// <returns></returns>
        public static DataTable GetDtBlueprintBomByBlueprintID(string sBlueprintID)
        {
            DataTable dt = new DataTable();
            string sql = @"select bb.*,mp.PRODUCT_CODE,mp.DESCRIPTION from BPT_BLUEPRINT bb
                left join MST_PRODUCT mp on bb.PRODUCT_ID=mp.PRODUCT_ID
                where bb.PARENT_ID='" + sBlueprintID + "' ";
            sql += " and bb.DELETEF_LAG=0 ";
            sql += "order by bb.SEQUENCE ";
            dt = DataAccess.SelectDataSet(sql).Tables[0];

            MstProductEntity mParentProduct=MstProductBs.GetMstProductByBlueprintID(sBlueprintID);
            if(mParentProduct==null)
                return null;
            DataTable dtProductBom = GetDtProductBomByProductID("","","");
            DataRow drNew = dtProductBom.NewRow();
            drNew["BOM_ID"]=Guid.NewGuid().ToString();
            drNew["VERSION"]="1.0";
            drNew["PRODUCT_ID"]=mParentProduct.ProductId;
            drNew["ITEM_TYPE"] = "PDTYPE01"; //自制
            drNew["SEQENCE"]=0;
            drNew["UNIT_DOSE"]=1;
            drNew["STATUS"]="BOM_S00";//有效
            drNew["APPROVE_STATUS"]=0;//草稿
            drNew["IS_CUSTOMER_FOR"]=0;
            drNew["PRODUCT_NAME"] = mParentProduct.ProductName;
            drNew["PRODUCT_CODE"] = mParentProduct.ProductCode;
            drNew["DESCRIPTION"] = mParentProduct.Description;
            dtProductBom.Rows.Add(drNew);
            if (dt.Rows.Count != 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    DataRow drProductBom = dtProductBom.NewRow();
                    drProductBom["BOM_ID"] = Guid.NewGuid().ToString();
                    drProductBom["PARENT_BOM_ID"] = drNew["BOM_ID"];
                    drProductBom["VERSION"] = "1.0";
                    drProductBom["PRODUCT_ID"]=dr["PRODUCT_ID"];
                    drProductBom["ITEM_TYPE"] = "PDTYPE01"; //自制
                    drProductBom["SEQENCE"] = dr["SEQUENCE"];
                    drProductBom["UNIT_DOSE"] = dr["MATERIAL_QTY"];
                    drProductBom["STATUS"] = "BOM_S00";//有效
                    drProductBom["IS_CUSTOMER_FOR"] = 0;
                    drProductBom["PRODUCT_NAME"] = dr["BLUEPRINT_NAME"];
                    drProductBom["PRODUCT_CODE"] = dr["PRODUCT_CODE"];
                    drProductBom["DESCRIPTION"] = dr["DESCRIPTION"];
                    dtProductBom.Rows.Add(drProductBom);
                }
            }
            return dtProductBom;
        }

        /// <summary>
        /// 保存生产BOM
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="deleteList"></param>
        /// <returns></returns>
        public static bool SaveProductBom(DataTable dt, List<BomProductBomMasterEntity> deleteList)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    List<BomProductBomMasterEntity> insertList = new List<BomProductBomMasterEntity>();
                    List<BomProductBomMasterEntity> modifyList = new List<BomProductBomMasterEntity>();
                    foreach (DataRow dr in dt.Rows)
                    {
                        BomProductBomMasterEntity entity = new BomProductBomMasterEntity();
                        switch (dr.RowState)
                        {
                            case DataRowState.Added:
                                entity = new BomProductBomMasterEntity(dr);
                                insertList.Add(entity);
                                break;
                            case DataRowState.Modified:
                                entity = new BomProductBomMasterEntity(dr);
                                modifyList.Add(entity);
                                break;
                            default:
                                continue;
                        }
                    }
                    DataAccess.Delete<BomProductBomMasterEntity>(deleteList, broker);
                    DataAccess.Update<BomProductBomMasterEntity>(modifyList, broker);
                    DataAccess.Insert<BomProductBomMasterEntity>(insertList, broker);
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }

        /// <summary>
        /// 查询产品对应BOM和版本
        /// </summary>
        /// <param name="sProductID"></param>
        /// <returns></returns>
        public static DataTable GetDtBomVersionByProductID(string sProductID)
        {
            string sql = @"select distinct bpbm.BOM_ID,bpbm.VERSION 
                from BOM_PRODUCT_BOM_MASTER bpbm
                where bpbm.PARENT_BOM_ID is null
                and bpbm.PRODUCT_ID='" + sProductID + "' ";
            return DataAccess.SelectDataSet(sql).Tables[0];
        }

        /// <summary>
        /// 是否存在BOM版本
        /// </summary>
        /// <param name="sProductID"></param>
        /// <param name="sVersion"></param>
        /// <returns></returns>
        public static bool IsExistVersion(string sProductID, string sVersion)
        {
            string sql = @"select distinct bpbm.BOM_ID,bpbm.VERSION 
                from BOM_PRODUCT_BOM_MASTER bpbm
                where bpbm.PARENT_BOM_ID is null
                and bpbm.PRODUCT_ID='" + sProductID + "' ";
            sql += "and bpbm.VERSION='"+sVersion+"' ";
            DataTable dt = DataAccess.SelectDataSet(sql).Tables[0];
            if (dt.Rows.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion

    }


}

